package com.utc.teamcenter.common.app.dialog.bean;

import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class BeanJTable extends BeanComponent {

	private JTable field;
	private AbstractTableModel tableModel;
	private JScrollPane scrollPane;
	
	public BeanJTable(String key, String name, AbstractTableModel tableModel) {
		super(key, name);

		this.field = new JTable();
		this.field.setName(key);
		this.tableModel = tableModel;
		this.field.setModel(this.tableModel);
		
		this.scrollPane = new JScrollPane(this.field);
	}
	
	public void setTableSize(int width, int height) {
		field.setPreferredScrollableViewportSize(new Dimension(width, height));
	}
	
	public void setColumnWidth(int column, int width) {
		field.getColumnModel().getColumn(column).setPreferredWidth(width);
//		field.getColumnModel().getColumn(column).setMinWidth(width);
//		field.getColumnModel().getColumn(column).setMaxWidth(width);
	}

	public int addBeanToPanel(JPanel panel, int i) {
		panel.add(i + ".1.right.top", super.getLabel());
		panel.add(i + ".2.left.top", this.scrollPane);

		return i;
	}

	@Override
	public void clear() {
		// this.field.removeAll();//untested
	}

	@Override
	public JComponent getComponent() {
		return this.field;
	}

	@Override
	public String getText() {
		return this.field.getSelectedRow() + "";
	}

	@Override
	public boolean isRequired() {
		return false;
	}

	@Override
	public void setText(String text) {
	}

	@Override
	public void setRequired(boolean required) {
	}

	@Override
	public void requestFocus() {
		this.field.requestFocus();
	}

	@Override
	public void setEnabled(boolean bool) {
		this.field.setEnabled(bool);
	}

	@Override
	public void setVisible(boolean bool) {
		this.field.setVisible(bool);
	}
}
